package ui;

import core.PlayerBoard;
import model.BasicBlockSlot;
import model.BlockType;
import org.apache.log4j.Logger;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * This is a test UI class to test how the game board would be setup.
 */
public class UI extends JPanel {

    private int slot_size; // should be in pixels
    private PlayerBoard board;
    private Logger logger = Logger.getLogger(UI.class);
    private Dimension ui_size;
    private int[] cursor;

    public UI() {
        super();
        slot_size = 20;
        ui_size = new Dimension(0, 0);
        cursor = new int[]{5, 10};
        setFocusable(true);
        init();
    }

    private void init() {
        requestFocus();
        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent keyEvent) {
            }

            @Override
            public void keyPressed(KeyEvent keyEvent) {
                if (keyEvent.getKeyCode() == KeyEvent.VK_DOWN) {
                    moveCursor(0, 1);
                } else if (keyEvent.getKeyCode() == KeyEvent.VK_UP) {
                    moveCursor(0, -1);
                } else if (keyEvent.getKeyCode() == KeyEvent.VK_LEFT) {
                    moveCursor(-1, 0);
                } else if (keyEvent.getKeyCode() == KeyEvent.VK_RIGHT) {
                    moveCursor(1, 0);
                } else if (keyEvent.getKeyCode() == KeyEvent.VK_SPACE) {
                    board.switchIndex(cursor);
                }
                repaint();
            }

            @Override
            public void keyReleased(KeyEvent keyEvent) {
            }
        });
    }

    public void displayBoard(PlayerBoard playerBoard) {
        board = playerBoard;
        ui_size = new Dimension(slot_size * board.getColumns(), slot_size * board.getRows());
        setPreferredSize(ui_size);
    }

    /**
     * Should just move the cursor left or right.
     *
     * @param hora
     * @param vert
     */
    public void moveCursor(int hora, int vert) {
        if (cursor[0] + hora >= 0 && cursor[0] + 1 + hora < board.getColumns()) {
            cursor[0] += hora;
        }
        if (cursor[1] + vert >= 0 && cursor[1] + vert < board.getRows()) {
            cursor[1] += vert;
        }
    }

    @Override
    public void paintComponent(Graphics graphics) {
        // display blocks.
        for (int i = 0; i < board.getRows(); i++) {
            for (int j = 0; j < board.getColumns(); j++) {
                // If slot has block, display the img/color.
                if (board.getSlot(i, j) instanceof BasicBlockSlot) {
                    BasicBlockSlot box = (BasicBlockSlot) board.getSlot(i, j);
                    graphics.setColor(box.getBlockType().getColor());
                    int x = i * slot_size;
                    int y = j * slot_size;

                    // fill in the block
                    graphics.fillRect(y, x, slot_size, slot_size);

                    // Should remove once icons are available.
                    if (box.getBlockType() != BlockType.BLANK) {
                        graphics.setColor(Color.BLACK);
                        graphics.drawRect(y, x, slot_size, slot_size);
                    }

                    // filling in text for testing
//                    graphics.setColor(Color.black);
//                    graphics.setFont(new Font("Dialog", 8, 8));
//                    graphics.drawString("" + i + "," + j, y + slot_size / 4, x + slot_size / 2);
                } else {
                    logger.debug("Unknown slot type " + board.getSlot(i, j).getClass().getName());
                }
            }
            graphics.drawRect(0, 0, slot_size * board.getColumns(), slot_size * board.getRows());
            paintCursor(graphics);
        }
    }

    /**
     * Used to set the basic painted cursor
     *
     * @param g
     */
    private void paintCursor(Graphics g) {
        g.setColor(new Color(200, 100, 200));

        g.fillRect(cursor[0] * slot_size, cursor[1] * slot_size, 4, slot_size);
        g.fillRect(cursor[0] * slot_size, cursor[1] * slot_size, slot_size / 2, 4);

        g.fillRect(cursor[0] * slot_size + slot_size * 2 - 4, cursor[1] * slot_size, 4, slot_size);
        g.fillRect(cursor[0] * slot_size + slot_size * 3 / 2, cursor[1] * slot_size + slot_size - 4, slot_size / 2, 4);
    }

}